﻿#region Copyright & License
//
// Project: Quark Framework
// Project site: http://quark.codeplex.com
// See the copyright statement on the project site for more details.
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Quark.Profiler.Framework
{
    /// <summary>
    /// 需要进行分析测试的活动接口，每一个过程接口实例对应到一个<see cref="ProfilingTestActivity"/>实例
    /// </summary>
    public interface IProfilingActivity : IDisposable
    {
        /// <summary>
        /// 获取分析测试上下文实例，如果执行上下文置入，
        /// 即未调用过<see cref="IProfilingProcedure.ContextualizeIntoProfiler(IProfilingContext)"/>方法则返回空值
        /// </summary>
        IProfilingContext ProfilingContext
        {
            get;
        }

        /// <summary>
        /// 获取最后一次执行测试是否成功，如果未执行过测试则返回false
        /// </summary>
        //bool IsLastExecutionSucceed
        //{
        //    get;
        //    set;
        //}

        /// <summary>
        /// 获取最后一次执行测试的耗时(毫秒数)，如果未执行过测试则返回0
        /// </summary>
        //int LastExecutionTicks
        //{
        //    get;
        //    set;
        //}

        /// <summary>
        /// 初始化并将当前活动置入到分析测试环境中
        /// </summary>
        /// <param name="context">分析测试上下文</param>
        void ContextualizeIntoProfiler(IProfilingContext context);

        /// <summary>
        /// 执行活动
        /// </summary>
        /// <param name="dataCollection">活动数据与信息的共享存储集合</param>
        /// <param name="requestParams">请求信息</param>
        /// <param name="counter">性能计数</param>
        /// <returns>当前执行是否成功</returns>
        bool Execute(IDictionary<string, object> dataCollection, ProfilingRequest requestParams, ProfilingTestActivityCounter counter);
    }

    /// <summary>
    /// 用于提供与分析测试环境交互的上下文接口
    /// </summary>
    public interface IProfilingContext
    {
        /// <summary>
        /// 获取对应分析测试用例的UI代理
        /// </summary>
        /// <param name="testCase">分析测试用例</param>
        /// <returns>UI代理</returns>
        IProfilingVisualizer GetProfilingVisualizer(ProfilingTestCase testCase);
        /// <summary>
        /// 获取对应分析测试活动的UI代理
        /// </summary>
        /// <param name="activity">分析测试活动</param>
        /// <returns>UI代理</returns>
        IProfilingVisualizer GetProfilingVisualizer(IProfilingActivity activity);

        /// <summary>
        /// 记录一般日志
        /// </summary>
        /// <param name="message">日志消息</param>
        void LogInfo(object sender, string message);
        /// <summary>
        /// 记录分析相关的日志
        /// </summary>
        /// <param name="message">日志消息</param>
        void LogProfilingInfo(object sender, string message);
        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="message">日志消息</param>
        void LogError(object sender, string message);
        /// <summary>
        /// 记录异常日志
        /// </summary>
        /// <param name="message">日志消息</param>
        /// <param name="exception">发生的异常</param>
        void LogException(object sender, string message, Exception exception);
    }

    /// <summary>
    /// 用于显示分析测试进度与数据的UI代理接口
    /// </summary>
    public interface IProfilingVisualizer
    {
        /// <summary>
        /// 更新分析测试信息
        /// </summary>
        /// <param name="loopedTimes">当前已循环的分析测试次数</param>
        /// <param name="maxLoopTimes">最大的循环次数</param>
        /// <param name="lastLoopElapsedTimes">最后一次循环的耗时</param>
        void UpdateProfilingLoopInfo(long loopedTimes, long maxLoopTimes, int lastLoopElapsedTimes);
    }
}
